package com.aishua.fire;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket config() {
    	//全局请求异常
    	/*ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<Parameter>();  
        tokenPar.name("Authorization").description("访问login后返回的Authorization认证令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
        pars.add(tokenPar.build());*/
    	//全局返回异常
        //自定义异常信息
        ArrayList<ResponseMessage> responseMessages = new ArrayList<ResponseMessage>();
        responseMessages.add(new ResponseMessageBuilder().code(400).message("请求参数错误").build());
        responseMessages.add(new ResponseMessageBuilder().code(401).message("token认证失败").build());
        responseMessages.add(new ResponseMessageBuilder().code(402).message("指定的接口令牌头未添加或为空").build());
        responseMessages.add(new ResponseMessageBuilder().code(403).message("请求资源权限不够").build());
        responseMessages.add(new ResponseMessageBuilder().code(404).message("请求资源不存在").build());
        responseMessages.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());
        responseMessages.add(new ResponseMessageBuilder().code(510).message("请求参数错误").build());
        responseMessages.add(new ResponseMessageBuilder().code(-1).message("未知异常").build());
        
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aishua.fire.controller"))
                .paths(PathSelectors.any())
                .build()
//                .globalOperationParameters(pars)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages)
                ;
    }
    
    

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("应急预案管理系统API文档")
                .contact(new Contact("作者:lt", "访问地址:", "联系方式:"))
                .build();
    }
    //http://localhost:8080/dist/index.html
}